#include <cassert>
#include <iostream>
#include <string>

#include "../include/rocksdb/db.h"


int main(int argc, char* argv[])
{
    std::cout << "参数数量：" << argc << std::endl;
    std::cout << "hello" << std::endl;

    std::string key,table;
    for (int i=0; i<argc; i++)
    {
        std::string arg = argv[i];
        if (arg == "-k" && i+1 < argc)
        {
            key = argv[++i];
        }
        if (arg == "-t" && i + 1 < argc)
        {
            table = argv[++i];
        }
    }
    std::cout << "Key: " << key << std::endl;
    std::cout << "Table: " << table << std::endl;

    if (key.empty() || table.empty())
    {
        std::cout << "key or table is null" << std::endl;
        assert("key or table is null");
        return 0;
    }
    std::string strPath = "/root/branch/feature_dev_useraccount/RecordBackend/server/TestApi/rocksdb";
    
    std::vector<rocksdb::ColumnFamilyHandle*> vechandles;
    rocksdb::DB* m_db;
    rocksdb::Options options;
    // options.create_if_missing = true;
    options.error_if_exists = true;

    // 数据库已存在时获取列族
    std::vector<std::string> column_families;
    rocksdb::Status status = rocksdb::DB::ListColumnFamilies(options, strPath, &column_families);
    std::vector<rocksdb::ColumnFamilyDescriptor> column_families_descriptors;
    if (!status.ok())
    {
        log_error("OpenRocksDB open ListColumnFamilies strPath=%s status=%s", strPath.c_str(), status.ToString().c_str());

        column_families_descriptors.push_back(rocksdb::ColumnFamilyDescriptor(ROCKSDB_NAMESPACE::kDefaultColumnFamilyName, rocksdb::ColumnFamilyOptions()));

    }else
    {
        for (size_t i = 0; i < column_families.size(); i++)
        {
            column_families_descriptors.push_back(rocksdb::ColumnFamilyDescriptor(column_families[i], rocksdb::ColumnFamilyOptions()));
        }
    }
    status = rocksdb::DB::Open(options, strPath, column_families_descriptors, &vechandles, &m_db); //rocksdb::DB::Open(options, strPath, &db);

    if (!status.ok())
    {
        log_error("OpenRocksDB open column_families fail strPath=%s status=%s", strPath.c_str(), status.ToString().c_str());
        return false;
    }

    for (const auto& item : column_families)
    {
        std::cout << item << std::endl;
    }

    // std::string value;
    // rocksdb::Status s = db->Get(rocksdb::ReadOptions(), table, key, &value);
    // std::cout << value << std::endl;
    // if (s.ok()) s = db->Put(rocksdb::WriteOptions(), key2, value);
    // if (s.ok()) s = db->Delete(rocksdb::WriteOptions(), key1);

    std::cout << status.ok() << std::endl;
}